<template>
  <div class="elm-edit">
    <elm-page-bb>
      <el-form ref="model" :model="model" size="small" label-width="120px">
        <elm-row>
          <el-form-item label="物资名称" prop="material_name" :rules="input_rule">
            <elm-select-input :show-id="true" :code-array="selectCode" v-model="model.material_name" url="/business/wms/goods/GetSelectData" width="600px"></elm-select-input>
          </el-form-item>
          <el-form-item label="物资类别" prop="material_category_mc">
            <el-input v-model="model.material_category_mc" disabled></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="规格/型号" prop="specification">
            <el-input v-model="model.specification" disabled></el-input>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="model.unit" disabled></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="库存数量" prop="stock_quantity">
            <el-input v-model="resData.stock_quantity" disabled>
              <template slot="append"><div style="width: 25px;text-align: center;">{{model.unit}}</div></template>
            </el-input>
          </el-form-item>
          <el-form-item label="入库数量" prop="in_number"
                        :rules="[{trigger: 'blur' ,validator: validateNumber},{trigger: 'blur' ,validator: in_number_test}]">
            <el-input v-model="model.in_number">
              <template slot="append"><div style="width: 25px;text-align: center;">{{model.unit}}</div></template>
            </el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="入库时间" prop="in_time">
            <el-date-picker v-model="model.in_time"  type="datetime" value-format="yyyy-MM-dd HH-mm-ss" style="width: 100%"></el-date-picker>
          </el-form-item>
          <el-form-item label="入库员" prop="in_role">
            <elm-ei v-model="model.in_role"></elm-ei>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="采购订单号" prop="purchase_order_no">
            <el-input v-model="model.purchase_order_no"></el-input>
          </el-form-item>
          <el-form-item></el-form-item>
        </elm-row>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="model.remarks" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
      <template #bottom>
        <el-button type="success" size="small" @click="save">保存并继续</el-button>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      model: {
        id: elm.newGuid(),
        material_name:null,
        in_time:new Date(),
        in_role:null,
        update_time: new Date(),
        update_role:null,
        unit:null,
        specification:null,
        material_category:null,
        material_category_mc:null,
        in_number: 0,
      },
      ruleEnable:true,
      resData:{
        stock_quantity:null,
      },
      selectCode:[
        {
          label:'规格/型号',
          prop:'specification'
        },
      ],
    }
  },
  async created() {
    if(this.resData.stock_quantity==null||this.resData.stock_quantity==undefined)
      this.resData.stock_quantity=0;
    this.model.in_time=this.get_time();
    this.model.in_role=await this.get_user_ei();
  },
  destroyed() {
    this.request.callVue.tablebinding();
  },
  computed:{
    input_rule(){
      if(this.ruleEnable)
        return [{ required: true, message: '必须填写'}];
      else
        return [];
    }
  },
  methods: {
    async save() {
      this.ruleEnable=true;
      await this.getResData(this.model.material_name);
      await this.$refs["model"].validate();
      this.model.update_time = this.get_time();
      this.model.update_role = await this.get_user_ei();
      var res = await elm.post('/business/wms/material_in/Upsert', this.model);
      this.resData.stock_quantity+=Number(this.model.in_number);
      this.resData.last_update_date=this.get_time();
      var re = await elm.post('/business/wms/material/Upsert', this.resData);
      if (res == true || re == true) {
        this.v('保存成功,请继续。')
        this.ruleEnable=false;
        this.model = {
          id: elm.newGuid(),
          material_name: "",
          in_number: 0,
          in_time: this.model.in_time,
          in_role: this.model.in_role,
          purchase_order_no: this.model.purchase_order_no,
          remarks: this.model.remarks,
        }
      }
      else {
        this.error('保存失败，请稍后尝试。');
      }
    },
    async submit() {
      this.ruleEnable=true;
      await this.getResData(this.model.material_name);
      await this.$refs["model"].validate();
      this.model.update_time = this.get_time();
      this.model.update_role = await this.get_user_ei();
      var res = await elm.post('/business/wms/material_in/Upsert', this.model);
      this.resData.stock_quantity+=Number(this.model.in_number);
      this.resData.last_update_date=this.get_time();
      var re = await elm.post('/business/wms/material/Upsert', this.resData);
      if (res == true || re == true) {
        this.v('保存成功。')
        this.close();
        this.request.callVue.tablebinding();
      }
      else {
        this.error('保存失败，请稍后尝试。');
      }
    },
    get_time(){
      var now=new Date();
      return now.getFullYear() + '-'
          + (now.getMonth() + 1).toString().padStart(2, '0') + '-'
          + now.getDate().toString().padStart(2, '0')+ ' '
          + now.getHours().toString().padStart(2, '0') + ':'
          + now.getMinutes().toString().padStart(2, '0') + ':'
          + now.getSeconds().toString().padStart(2, '0');
    },
    async get_user_ei(){
      var res = await elm.post('/system/login/GetUserEi');
      //console.log(res);
      if(res.length>0)
        return res[0].emp_code;
      else
        return "";
    },
    in_number_test(rule, value, callback) {
      if (value<=0) {
        callback(new Error('必须为正数'));
      } else {
        callback();
      }
    },
    async getResData(Val){
      //console.log(Val+'\n');
      var res=await elm.post('/business/wms/material/GetData',{id:Val})
      //console.log("结果：",res);
      if(Val!=null&&Val!=""&&res.pagedata.length>0){
        this.resData=res.pagedata[0];
      }
      else {
        this.resData=this.model;
        this.resData.stock_quantity=0;
      }
    }
  },
  watch: {
    'model.material_name': async function(newVal) {
      //console.log(`material_name changed to: ${newVal}`);
      var res=await elm.post('/business/wms/goods/GetGoodsData', {id:newVal});
      //console.log("结果：",res);
      if(res.length>0){
        Object.assign(this.model, res[0]);
      }
      await this.getResData(newVal);
      //console.log(this.resData);
    },
  }
}
</script>